package com.itheima.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.EmpResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.core.Local;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/emps")
@Slf4j
public class EmpController {

    @Autowired
    private EmpService empService;

    //员工列表查询（分页查询）
    @GetMapping
    public Result Page(EmpQueryParam queryParam){
        log.info("分页查询，参数：{}", queryParam);
        return Result.success(empService.pageEmp(queryParam));
    }

    //添加员工
    @PostMapping
    public Result addEmp(@RequestBody Emp emp) {
        //填补员工基本信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        log.info("新增员工，员工信息：{}", emp);
        empService.insertEmp(emp);
        return Result.success();
    }

    //查询全部员工
    @GetMapping("/list")
    public Result getAllEmp() {
        log.info("查询全部员工");
        return Result.success(empService.list());
    }

    //删除员工(可批量)
    @DeleteMapping
    public Result deleteByIds(@RequestParam List<Integer> ids) {
        log.info("批量删除员工，员工id：{}", ids);
        empService.deleteEmp(ids);
        return Result.success();
    }

    //查询回显（根据id查询员工）
    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable Integer id) {
        log.info("查询回显，员工id：{}", id);
        Emp emp = empService.getEmpById(id);
        return Result.success(emp);
    }

    //修改员工信息
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp) {
        //修改员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        log.info("修改员工信息，员工信息：{}", emp);
        empService.updateEmp(emp);
        return Result.success();
    }
}
